const { fetchFriend, becomeFriend, destroyFriend, breakrelation, selectMyFriend, selectMyFollowFriend, MyFriends } = require('../services/FriendService')


class FriendController {

    async beFriend(ctx, next) {
        const { user_id } = ctx.state.user
        const { friendId } = ctx.request.body
        const res = await fetchFriend(user_id, friendId)
        ctx.body = {
            code: 0,
            message: 'success',
            result: "关注成功"
        }
    }
    async agreeFriend(ctx, next) {
        try {
            const { user_id } = ctx.state.user
            const { friendId } = ctx.request.body
            const res = await becomeFriend(user_id, friendId) > 0 ? "关注成功" : "出错了"
            ctx.body = {
                code: 0,
                message: 'success',
                result: res
            }
        } catch (error) {
            console.log(error);
        }
    }
    async ignoreFriend(ctx, next) {
        try {
            const { user_id } = ctx.state.user
            const { friendId } = ctx.request.body
            const { res, status } = await destroyFriend(user_id, friendId)
            console.log(status);
            ctx.body = {
                code: 0,
                message: "success",
                status: status,
                result: res[0] > 0 ? "取关成功" : "出错了"
            }
        } catch (error) {
            console.log(error);
        }
    }
    //取消关注单方面
    async breakFriend(ctx, next) {
        const { user_id } = ctx.state.user
        const { friendId } = ctx.request.body
        const res = await breakrelation(user_id, friendId)
        if (res > 0) {
            return ctx.body = {
                code: 0,
                message: 'success',
                result: "取消关注"
            }
        }
        else {
            return ctx.body = {
                code: 200,
                message: 'success',
                result: ""
            }
        }


    }

    //关注我的好友查询
    async followMyFriend(ctx, next) {
        const { user_id: userId } = ctx.state.user
        const { pageNo, pageSize } = ctx.request.query

        const res = await selectMyFriend({ userId, pageNo, pageSize })
        ctx.body = {
            code: 0,
            message: "success",
            result: res.result
        }
    }

    //查询我关注的好友
    async myFollowFriend(ctx, next) {
        const { user_id: userId } = ctx.state.user
        const { pageNo, pageSize } = ctx.request.query

        const res = await selectMyFollowFriend({ userId, pageNo, pageSize })
        ctx.body = {
            code: 0,
            message: "success",
            result: res.result
        }
    }

    //查询我的好友
    async myFriend(ctx, next) {
        const { user_id: userId } = ctx.state.user
        const { pageNo, pageSize } = ctx.request.query

        const res = await MyFriends({ userId, pageNo, pageSize })
        ctx.body = {
            code: 0,
            message: "success",
            result: res.result,
            count: res.count
        }
    }
}
module.exports = new FriendController()